//获取应用实例
const app = getApp();
// 引入AES.js
var baseJs = require("../../common/base.js");
// 引入SDK核心类,实例化API核心类
var QQMapWX = require('../../common/qqmap-wx-jssdk.js');
var demo = new QQMapWX({
  key: 'b6af1635786ff310b0f07cba63460532' // 必填
});
// 二维码
var QR = require("../../common/qrcode.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrl: baseJs.imgUrl,
    orderId: "",
    orderDetailData: [],
    map: "",
    // 兑换点信息
    checkFlag: true,
    // 二维码
    maskFlag: true,
    codeView: true,
    url: "",
    // 
    uid:"",
    userType:"",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // console.log(options);
    this.setData({
      uid: wx.getStorageSync('uid'),
      userType: wx.getStorageSync('userType'),
      orderId: options.orderId
    })
    this.GetOrderDetail();
    //动态设置画布大小
    var size = this.setCanvasSize();
  },

  // 
  GetOrderDetail: function() {
    var that = this;
    var uid = this.data.uid;
    var orderId = this.data.orderId;
    var orderDetailData = this.data.orderDetailData;
    var map = this.data.map;
    var url = this.data.url;
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=GetOrderDetail&UID=" + uid + "&OrderID=" + orderId + "&type=0"),
      success: function(res) {
        console.log(JSON.parse(baseJs.decryUrl(res.data)).ResultData);
        map = JSON.parse(baseJs.decryUrl(res.data)).ResultData.GoodsList[0].ExchangeAddress;
        url = JSON.parse(baseJs.decryUrl(res.data)).ResultData.ConsumerNumber;
        that.setData({
          orderDetailData: JSON.parse(baseJs.decryUrl(res.data)).ResultData,
          map: map,
          url: "exchange&" + url,
        })
      }
    })
  },
  // exchangeInfo
  exchangeInfoTap: function() {
    this.setData({
      checkFlag: !this.data.checkFlag
    });
  },
  // 导航
  openMap: function() {
    var that = this;
    var map = this.data.map;
    // 调用接口
    demo.geocoder({
      address: map,
      success: function(res) {
        console.log(res);
        wx.openLocation({
          latitude: res.result.location.lat,
          longitude: res.result.location.lng,
          scale: 28
        })
      },
      fail: function(res) {
        console.log(res);
      },
      complete: function(res) {
        console.log(res);
      }
    });

  },
  // 
  phoneCall: function() {
    var that = this;
    var orderDetailData = this.data.orderDetailData;
    var phoneNumber = orderDetailData.DuihuanPhone;
    wx.makePhoneCall({
      phoneNumber: phoneNumber
    })
  },
  // maskTap点击蒙版
  maskTap:function(){
    this.setData({
      maskFlag: true,
      codeView: true,
    })
  },
  // codeShow二维码
  codeShow: function() {
    var that = this;
    this.setData({
      maskFlag: false,
      codeView: false,
    })
    that.createCodeImg();

  },
  //适配不同屏幕大小的canvas
  setCanvasSize: function() {
    var size = {};
    try {
      var res = wx.getSystemInfoSync();
      var scale = 750 / 400; //不同屏幕下canvas的适配比例；设计稿是750宽
      //canvas画布为正方形
      var width = res.windowWidth / scale;
      var height = width;
      size.w = width;
      size.h = height;
    } catch (e) {
      // Do something when catch error
      console.log("获取设备信息失败" + e);
    }
    return size;
  },
  //获取临时缓存照片路径，存入data中
  canvasToTempImage: function() {
    var that = this;
    wx.canvasToTempFilePath({
      canvasId: 'mycanvas',
      success: function(res) {
        var tempFilePath = res.tempFilePath;
        // console.log(tempFilePath);
        that.setData({
          imagePath: tempFilePath,
          // canvasHidden:true
        });
      },
      fail: function(res) {
        // console.log(res);
      }
    });
  },
  //点击图片进行预览，长按保存分享图片
  previewImg: function(e) {
    var img = this.data.imagePath;
    // console.log(img);
    wx.previewImage({
      current: img, // 当前显示图片的http链接
      urls: [img] // 需要预览的图片http链接列表
    })
  },
  // 
  createQrCode: function(url, canvasId, cavW, cavH) {
    //调用插件中的draw方法，绘制二维码图片
    QR.api.draw(url, canvasId, cavW, cavH);
    setTimeout(() => {
      this.canvasToTempImage();
    }, 1000);

  },
  // createCodeImg
  createCodeImg: function() {
    var that = this;
    var url = this.data.url;
    // console.log(url);
    wx.showToast({
      title: '生成中...',
      icon: 'loading',
      duration: 2000
    });
    var st = setTimeout(function() {
      wx.hideToast()
      var size = that.setCanvasSize();
      //绘制二维码
      that.createQrCode(url, "mycanvas", size.w, size.h);
      clearTimeout(st);
    }, 2000)
  }
})